package cease.update;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.MessageFormat;

import cease.butter.Constants;
import cease.http.HttpRequestExecutor;
import cease.http.HttpRequestResult;

/**
 * @author dhf
 */
public class Updater {
    private Updater() {}

    private static HttpRequestExecutor executor = new HttpRequestExecutor();

    public static boolean hasNewVersion() throws UpdateException {
        Version current = Version.getCurrentVersion();
        Version latest = Version.getLastestVersion();
        if (latest.getMajorVersion() > current.getMajorVersion()) {
            return true;
        }
        if (latest.getSubVersion() > current.getSubVersion()) {
            return true;
        }
        if (latest.getRevision() > current.getRevision()) {
            return true;
        }
        return false;
    }

    public static byte[] download(Version version) throws UpdateException {
        String url = getDownloadUrl(version);
        try {
            HttpRequestResult result = executor.get(url, null, null, url);
            if (result.getStatus() == 200) {
                return result.getContent();
            } else {
                throw new UpdateException("Http Status Code : "
                        + result.getStatus());
            }
        } catch (IOException e) {
            throw new UpdateException(e);
        }
    }

    private static String getDownloadUrl(Version version) {
        return MessageFormat.format(Constants.APP_DOWNLOAD_URL, version);
    }

    public static void download(Version version, String directory) {
        DownloadThread t = new DownloadThread(version, directory);
        t.start();
    }

    private static class DownloadThread extends Thread {
        private Version version;

        private String directory;

        public DownloadThread(Version version, String directory) {
            this.version = version;
            this.directory = directory;
        }

        @Override
        public void run() {
            try {
                File f = new File(directory);
                if (f.exists()) {
                    if (!f.isDirectory()) {
                        System.out.println("[" + directory
                                + "] is not a directory");
                        return;
                    }
                } else {
                    System.out
                            .println("creating directory [" + directory + "]");
                    f.mkdirs();
                }
                f = new File(directory
                        + File.separator
                        + MessageFormat.format(Constants.APP_DOWNLOAD_FILENAME,
                                version));
                System.out.println("start downloading ["
                        + getDownloadUrl(version) + "]");
                byte[] bytes = download(version);
                f.deleteOnExit();
                f.createNewFile();
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(f);
                    fos.write(bytes);
                    System.out.println("download [" + getDownloadUrl(version)
                            + "] done");
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        fos.close();
                    } catch (IOException e) {}
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
